#include "magic_tower.h"
#include "ui_magic_tower.h"
#include <QRect>
#include <QPixmap>
#include <QPainter>
#include <QFont>
Place map[3][13][13];
Monster monster[15];
magic_tower::magic_tower(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::magic_tower)
{
    ui->setupUi(this);
    this->setGeometry(QRect(50,50,1200,810));

}

magic_tower::~magic_tower()
{
    delete ui;
}
void magic_tower::paintEvent(QPaintEvent *event)
{
    if(!IsRun)
    {
        InitBraver();
        InitMap();
    }
    showthings();

    QPainter painter(this);
    //背景
    painter.setPen(Qt::black);
    painter.setBrush(Qt::gray);
    painter.drawRect(350,120,550,550);
    //格子
    painter.setPen(Qt::black);
    for(int i=1;i<=10;i++)
    {
        painter.drawLine(350,120+i*50,900,120+i*50);
    }
    for(int i=1;i<=10;i++)
    {
        painter.drawLine(350+i*50,120,350+i*50,670);
    }
    //画勇者
    painter.setPen(Qt::black);
    painter.setBrush(Qt::green);
    painter.drawRect(pos);
    QFont font1("Courier",30);
    painter.setFont(font1);
    painter.setPen(Qt::darkRed);
    painter.setBrush(Qt::darkRed);
    painter.drawText(300+posy*50,120+posx*50,name);
    //画墙
    for(int i=0;i<13;i++)
    {
        for(int j=0;j<13;j++)
        {
            if(map[floor][i][j].cover==1)
            {
                painter.setPen(Qt::black);
                painter.setBrush(Qt::darkYellow);
                painter.drawRect(map[floor][i][j].rect);
            }
            if(map[floor][i][j].cover==2)
            {
                painter.setPen(Qt::black);
                painter.setBrush(Qt::white);
                painter.drawRect(map[floor][i][j].rect);
                painter.setPen(Qt::red);
                painter.drawText(300+j*50,120+i*50,map[floor][i][j].name);
            }
            if(map[floor][i][j].cover==3)
            {
                painter.setPen(Qt::black);
                painter.setBrush(Qt::white);
                painter.drawRect(map[floor][i][j].rect);
                painter.setPen(Qt::blue);
                painter.drawText(300+j*50,120+i*50,map[floor][i][j].name);
            }
            if(map[floor][i][j].cover==4)
            {
                painter.setPen(Qt::black);
                painter.setBrush(Qt::white);
                painter.drawRect(map[floor][i][j].rect);
                painter.setPen(Qt::magenta);
                painter.drawText(300+j*50,120+i*50,map[floor][i][j].name);
            }
            if(map[floor][i][j].cover==5)
            {
                painter.setPen(Qt::black);
                painter.setBrush(Qt::white);
                painter.drawRect(map[floor][i][j].rect);
                painter.setPen(Qt::blue);
                painter.drawText(300+j*50,120+i*50,map[floor][i][j].name);
            }
            if(map[floor][i][j].cover==8)
            {
                painter.setPen(Qt::black);
                painter.setBrush(Qt::white);
                painter.drawRect(map[floor][i][j].rect);
                painter.setPen(Qt::darkYellow);
                painter.drawText(300+j*50,120+i*50,map[floor][i][j].name);
            }
            if(map[floor][i][j].cover==9)
            {
                painter.setPen(Qt::black);
                painter.setBrush(Qt::white);
                painter.drawRect(map[floor][i][j].rect);
                painter.setPen(Qt::darkBlue);
                painter.drawText(300+j*50,120+i*50,map[floor][i][j].name);
            }
            if(map[floor][i][j].cover==10)
            {
                painter.setPen(Qt::black);
                painter.setBrush(Qt::white);
                painter.drawRect(map[floor][i][j].rect);
                painter.setPen(Qt::darkRed);
                painter.drawText(300+j*50,120+i*50,map[floor][i][j].name);
            }
            if(map[floor][i][j].cover==80)
            {
                painter.setPen(Qt::black);
                painter.setBrush(Qt::yellow);
                painter.drawRect(map[floor][i][j].rect);
            }
            if(map[floor][i][j].cover==90)
            {
                painter.setPen(Qt::black);
                painter.setBrush(Qt::blue);
                painter.drawRect(map[floor][i][j].rect);
            }
            if(map[floor][i][j].cover==100)
            {
                painter.setPen(Qt::black);
                painter.setBrush(Qt::red);
                painter.drawRect(map[floor][i][j].rect);
            }
            if(map[floor][i][j].cover==11)
            {
                painter.setPen(Qt::black);
                painter.setBrush(Qt::gray);
                painter.drawRect(map[floor][i][j].rect);
                painter.setPen(Qt::black);
                painter.drawText(300+j*50,120+i*50,map[floor][i][j].name);
            }
            if(map[floor][i][j].cover==20)
            {
                painter.setPen(Qt::black);
                painter.setBrush(Qt::white);
                painter.drawRect(map[floor][i][j].rect);
                painter.setPen(Qt::darkRed);
                painter.drawText(300+j*50,120+i*50,map[floor][i][j].mon1->name);

            }

        }
    }
}
void magic_tower::InitBraver()
{
    posx=10;
    posy=6;

    floor=0;
    level=1;
    hp=1000;
    atk=10;
    pdef=10;
    name="勇";
    exp=gold=0;key1=1;key2=key3=1;
    QRect rect(300+posy*50,70+posx*50,50,50);
    pos=rect;
}
void magic_tower::keyPressEvent(QKeyEvent *event)
{
    IsRun=1;
    QKeyEvent *key=(QKeyEvent*)event;
    switch (key->key())
    {
    case Qt::Key_Up:
        pos.setTop(pos.top()-50);
        pos.setBottom(pos.bottom()-50);
        posx--;
        if(map[floor][posx][posy].cover!=0)
        {
            if(map[floor][posx][posy].cover!=1)
            {
                Pickup();
            }
            if(IsFlooring)
            {IsFlooring=0;}
            else
            {
                pos.setTop(pos.top()+50);
                pos.setBottom(pos.bottom()+50);
                posx++;
            }

        }
        break;
    case Qt::Key_Down:
        pos.setTop(pos.top()+50);
        pos.setBottom(pos.bottom()+50);
        posx++;
        if(map[floor][posx][posy].cover!=0)
        {
            if(map[floor][posx][posy].cover!=1)
            {
                Pickup();

            }
            if(IsFlooring)
            {IsFlooring=0;}
            else
            {
                pos.setTop(pos.top()-50);
                pos.setBottom(pos.bottom()-50);
                posx--;
            }

        }
        break;
    case Qt::Key_Left:
        pos.setLeft(pos.left()-50);
        pos.setRight(pos.right()-50);
        posy--;
        if(map[floor][posx][posy].cover!=0)
        {
            if(map[floor][posx][posy].cover!=1&&map[floor][posx][posy].cover!=20)
            {
                Pickup();

            }
            if(map[floor][posx][posy].cover==20)
            {
                Fight(map[floor][posx][posy].mon1);
                map[floor][posx][posy].cover=0;
                if(hp<1)IsOver=1;
            }
            if(IsFlooring)
            {IsFlooring=0;}
            else
            {
                pos.setLeft(pos.left()+50);
                pos.setRight(pos.right()+50);
                posy++;
            }

        }
        break;
    case Qt::Key_Right:
        pos.setLeft(pos.left()+50);
        pos.setRight(pos.right()+50);
        posy++;
        if(map[floor][posx][posy].cover!=0)
        {
            if(map[floor][posx][posy].cover!=1)
            {
                Pickup();
            }
            if(IsFlooring)
            {IsFlooring=0;}
            else
            {
                pos.setLeft(pos.left()-50);
                pos.setRight(pos.right()-50);
                posy--;
            }

        }
        break;
    default:
        break;
    }
    repaint();
}
void magic_tower::InitMap()
{
    for(int k=0;k<3;k++)
    {
        for(int i=0;i<13;i++)
        {
            for(int j=0;j<13;j++)
            {
                QRect temp(300+50*j,70+50*i,50,50);
                map[k][i][j].rect=temp;
            }
        }
    }

    //手动画墙
    LoadMap();
    LoadMonster();
}
void magic_tower::showthings()
{
    ui->label_3->setText(QString::number(hp));
    ui->label_4->setText(QString::number(level));
    ui->label_5->setText(QString::number(atk));
    ui->label_6->setText(QString::number(pdef));
    ui->label_7->setText(QString::number(gold));
    ui->label_8->setText(QString::number(exp));
    ui->label_9->setText(QString::number(key1));
    ui->label_10->setText(QString::number(key2));
    ui->label_11->setText(QString::number(key3));
}
void magic_tower::Pickup()
{
        if(map[floor][posx][posy].cover==2)//小血瓶
        {hp+=200;map[floor][posx][posy].cover=0;}
        if(map[floor][posx][posy].cover==3)//大
        {hp+=500;map[floor][posx][posy].cover=0;}
        if(map[floor][posx][posy].cover==4)//红
        {atk+=3;map[floor][posx][posy].cover=0;}
        if(map[floor][posx][posy].cover==5)//蓝
        {pdef+=3;map[floor][posx][posy].cover=0;}
        if(map[floor][posx][posy].cover==8)
        {key1+=1;map[floor][posx][posy].cover=0;}
        if(map[floor][posx][posy].cover==9)
        {key2+=1;map[floor][posx][posy].cover=0;}
        if(map[floor][posx][posy].cover==10)
        {key3+=1;map[floor][posx][posy].cover=0;}
        if(map[floor][posx][posy].cover==80&&key1>0)
        {key1--;map[floor][posx][posy].cover=0;}
        if(map[floor][posx][posy].cover==90&&key2>0)
        {key2--;map[floor][posx][posy].cover=0;}
        if(map[floor][posx][posy].cover==100&&key3>0)
        {key3--;map[floor][posx][posy].cover=0;}
        if(floor==0&&posx==1&&posy==6)
        {
            IsFlooring=1;
            floor=1;posx=10;
            QRect rect(300+posy*50,70+posx*50,50,50);
            pos=rect;
        }
        if(floor==1&&posx==11&&posy==6)
        {
            IsFlooring=1;
            floor=0;posx=2;posy=6;
            QRect rect(300+posy*50,70+posx*50,50,50);
            pos=rect;
        }
        if(floor==1&&posx==1&&posy==1)
        {
            IsFlooring=1;
            floor=2;posx=6;posy=6;
            QRect rect(300+posy*50,70+posx*50,50,50);
            pos=rect;
        }
}
void magic_tower::LoadMap()
{

    for(int j=0;j<13;j++)
        {
            map[1][12][j].cover=1;
            map[1][0][j].cover=1;
            map[1][j][12].cover=1;
            map[1][j][0].cover=1;
        }
        for(int i=1;i<=10;i++)
        {map[1][2][i].cover=1;}
        map[1][3][6].cover=map[1][3][10].cover=1;
        map[1][4][4].cover=map[1][4][6].cover=map[1][4][10].cover=1;
        map[1][5][1].cover=map[1][5][3].cover=map[1][5][4].cover=map[1][5][6].cover=map[1][5][7].cover=map[1][5][8].cover=map[1][5][10].cover=1;
        map[1][6][4].cover=map[1][6][10].cover=1;map[1][7][4].cover=1;
        for(int i=6;i<=10;i++)
        {map[1][7][i].cover=1;}
        map[1][8][1].cover=map[1][8][3].cover=map[1][8][4].cover=1;
        map[1][9][4].cover=map[1][9][5].cover=map[1][9][7].cover=map[1][9][8].cover=map[1][9][9].cover=map[1][9][11].cover=1;
        map[1][10][4].cover=map[1][10][8].cover=map[1][11][4].cover=map[1][11][8].cover=1;
        map[1][3][1].cover=map[1][3][7].cover=map[1][3][9].cover=map[1][4][7].cover=map[1][4][9].cover=map[1][10][1].cover=map[1][11][1].cover=2;
        map[1][10][2].cover=3;
        map[1][4][3].cover=4;map[1][4][1].cover=map[1][3][8].cover=map[1][4][8].cover=map[1][6][1].cover=map[1][10][3].cover=map[1][11][3].cover=8;
        map[1][10][9].cover=map[1][11][9].cover=map[1][11][10].cover=map[1][11][11].cover=map[1][1][3].cover=8;
        map[1][7][3].cover=map[1][10][11].cover=9;map[1][10][5].cover=10;map[1][7][1].cover=5;
        map[1][3][4].cover=map[1][5][2].cover=map[1][6][6].cover=map[1][8][2].cover=map[1][9][10].cover=map[0][8][6].cover=80;
        map[1][9][6].cover=100;
        for(int i=0;i<13;i++)
        {
            for(int j=0;j<13;j++)
            {
                map[0][i][j].cover=1;
                map[2][i][j].cover=1;
            }
        }
        for(int i=2;i<=11;i++)
        {map[0][i][6].cover=0;}
        map[0][1][6].cover=map[1][11][6].cover=11;
        map[0][9][5].cover=map[0][9][7].cover=0;
        map[0][8][6].cover=80;
        map[2][2][3].cover=map[2][2][4].cover=map[2][2][6].cover=map[2][2][8].cover=map[2][2][9].cover=0;
        map[2][3][2].cover=map[2][3][3].cover=map[2][3][6].cover=map[2][3][9].cover=map[2][3][10].cover=0;
        map[2][4][2].cover=map[2][4][3].cover=map[2][4][4].cover=map[2][4][6].cover=map[2][4][8].cover=map[2][4][9].cover=map[2][4][10].cover=0;
        for(int i=3;i<=9;i++)
        {
            map[2][5][i].cover=0;
            map[2][6][i].cover=0;
        }
        map[2][7][4].cover=map[2][7][5].cover=map[2][7][7].cover=map[2][7][8].cover=0;
        map[2][8][6].cover=map[1][1][1].cover=11;
        map[1][1][4].cover=map[1][1][5].cover=map[1][1][6].cover=map[1][3][3].cover=map[1][4][2].cover=20;
        map[1][5][9].cover=map[1][6][2].cover=map[1][6][7].cover=map[1][6][8].cover=map[1][6][9].cover=map[1][9][2].cover=map[1][10][10].cover=20;
        for(int f=0;f<3;f++)
        {
            for(int i=0;i<13;i++)
            {
                for(int j=0;j<13;j++)
                {
                    if(map[f][i][j].cover==2)
                    {map[f][i][j].name="小血";}
                    if(map[f][i][j].cover==3)
                    {map[f][i][j].name="大血";}
                    if(map[f][i][j].cover==4)
                    {map[f][i][j].name="宝";}
                    if(map[f][i][j].cover==5)
                    {map[f][i][j].name="宝";}
                    if(map[f][i][j].cover==8)
                    {map[f][i][j].name="钥";}
                    if(map[f][i][j].cover==9)
                    {map[f][i][j].name="钥";}
                    if(map[f][i][j].cover==10)
                    {map[f][i][j].name="钥";}
                    if(map[f][i][j].cover==11)
                    {map[f][i][j].name="台阶";}
                }
            }
        }

}
void magic_tower::Fight(Monster *mon)
{
    while(hp>0&&mon->hp>0)
    {
        hp=hp-(mon->atk-pdef);
        mon->hp=mon->hp-(atk-mon->pdef);
    }
    gold+=mon->gold;
    exp+=mon->exp;

}
void magic_tower::LoadMonster()
{
    map[1][1][4].mon1=&(monster[1]);monster[1].num=1;
    map[1][1][6].mon1=&(monster[2]);monster[2].num=1;
    map[1][6][8].mon1=&(monster[3]);monster[3].num=1;
    for(int i=0;i<15;i++)
    {
        if(monster[i].num==1)
        {
            monster[i].name="史";
            monster[i].atk=20;
            monster[i].pdef=1;
            monster[i].gold=monster[i].exp=1;
            monster[i].hp=50;
        }
    }
}
Place::Place()
{
    mon1=new Monster;
}
Place::~Place()
{
    delete mon1;
}
